Passed
Branch feature/phase1 (a6d58a)
by Pieter Epeüs
09:26 queued 43s
created

getByKey.js ➔ getByKey   A

Complexity

Conditions 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 9.75
c 0
b 0
f 0
cc 3
1
export default function getByKey(original, key, defaultValue) {
2
    const keys = key.split('.');
3
4
    let reference = original;
5
6
    while (keys.length > 0) {
7
        const referenceKey = keys.shift();
8
9
        if (
10
            reference === null ||
11
            reference === undefined ||
12
            !Object.prototype.hasOwnProperty.call(reference, referenceKey)
13
        ) {
14
            return defaultValue;
15
        }
16
        reference = reference[referenceKey];
17
    }
18
19
    return reference;
20
}
21